﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OracleClient;
using PPAP.Model;

namespace PPAP.DAO
{
    public class TestDepartmentCardDAO
    {
        public TestDepartmentCardDAO() { }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(TestDepartmentCard model)
        {
            model.ID = DbHelperOra.GetMaxID("ID", "TESTDEPARTMENTCARD");
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into TESTDEPARTMENTCARD(");
            strSql.Append("ID,TESTDEPARTMENT,USERNAME)");
            strSql.Append(" values (");
            strSql.Append(":ID,:TESTDEPARTMENT,:USERNAME)");
            OracleParameter[] parameters = {
					new OracleParameter(":ID", OracleType.Number,4),
					new OracleParameter(":TESTDEPARTMENT", OracleType.Number,4),
					new OracleParameter(":USERNAME", OracleType.VarChar,150)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.TESTDEPARTMENT;
            parameters[2].Value = model.USERNAME;

           return DbHelperOra.ExecuteSql(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public int Update(TestDepartmentCard model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update TESTDEPARTMENTCARD set ");
            strSql.Append("ID=:ID,");
            strSql.Append("TESTDEPARTMENT=:TESTDEPARTMENT,");
            strSql.Append("USERNAME=:USERNAME");
            strSql.Append(" where id="+model.ID);
            OracleParameter[] parameters = {
					new OracleParameter(":ID", OracleType.Number,4),
					new OracleParameter(":TESTDEPARTMENT", OracleType.Number,4),
					new OracleParameter(":USERNAME", OracleType.VarChar,150)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.TESTDEPARTMENT;
            parameters[2].Value = model.USERNAME;

           return DbHelperOra.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据 删除所有改用户的分配
        /// </summary>
        public int Delete(string Username)
        {
            //该表无主键信息，请自定义主键/条件字段
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from TESTDEPARTMENTCARD ");
            strSql.Append(" where username='"+Username+"' ");
            OracleParameter[] parameters = {
};

            return DbHelperOra.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public TestDepartmentCard GetModel(string username)
        {
            //该表无主键信息，请自定义主键/条件字段
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,TESTDEPARTMENT,USERNAME from TESTDEPARTMENTCARD ");
            strSql.Append(" where username='"+username+"' ");
            OracleParameter[] parameters = {
};

            TestDepartmentCard model = new TestDepartmentCard();
            DataSet ds = DbHelperOra.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["TESTDEPARTMENT"].ToString() != "")
                {
                    model.TESTDEPARTMENT = int.Parse(ds.Tables[0].Rows[0]["TESTDEPARTMENT"].ToString());
                }
                model.USERNAME = ds.Tables[0].Rows[0]["USERNAME"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,TESTDEPARTMENT,USERNAME ");
            strSql.Append(" FROM TESTDEPARTMENTCARD ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperOra.Query(strSql.ToString());
        }
    }
}
